package com.kzj.controller;

import com.kzj.annotation.OnCreate;
import com.kzj.annotation.OnEdit;
import com.kzj.common.constant.GlobalConstant;
import com.kzj.common.enumerate.RoleEnum;
import com.kzj.common.exception.MyException;
import com.kzj.common.result.Result;
import com.kzj.pojo.College;
import com.kzj.pojo.Course;
import com.kzj.pojo.CourseStudent;
import com.kzj.pojo.Student;
import com.kzj.pojo.dto.CourseStudentBatchAddDto;
import com.kzj.pojo.vo.CourseStudentVo;
import com.kzj.service.CourseStudentService;
import com.kzj.service.jpaRepository.CollegeJpaRepository;
import com.kzj.service.jpaRepository.CourseJpaRepository;
import com.kzj.service.jpaRepository.CourseStudentJpaRepository;
import com.kzj.service.jpaRepository.StudentJpaRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/courseStudent")
public class CourseStudentController {
    @Autowired
    CourseStudentService courseStudentService;
    @GetMapping
    public Result getAll()
    {
        return courseStudentService.getAll();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id")Long id)
    {
        return courseStudentService.getById(id);
    }
    @PutMapping
    public Result update(@RequestBody @Validated(OnEdit.class) CourseStudent courseStudent)
    {
        return courseStudentService.update(courseStudent);
    }
    @PostMapping
    public Result add(@RequestBody @Validated(OnCreate.class) CourseStudent courseStudent)
    {
       return courseStudentService.add(courseStudent);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Long id)
    {
        return courseStudentService.deleteById(id);
    }
    @PostMapping("/batch")
    public Result batchAdd(@RequestBody CourseStudentBatchAddDto dto)
    {
        return courseStudentService.batchAdd(dto);
    }
    @GetMapping("/getOptionalCoursesByStudentId/{studentId}")
    public Result getOptionalCoursesByStudentId(@PathVariable("studentId") Long studentId)
    {
        return courseStudentService.getOptionalCoursesByStudentId(studentId);
    }
    @GetMapping("/getOptionalStudentsByCourseId/{courseId}")
    public Result getOptionalStudentsByCourseId(@PathVariable("courseId") Long courseId)
    {
        return courseStudentService.getOptionalStudentsByCourseId(courseId);
    }
    @DeleteMapping("/batch/{courseIds}/{studentIds}")
    public Result batchDelete(@PathVariable("courseIds")String courseIds,@PathVariable("studentIds")String studentIds)
    {
        return courseStudentService.batchDelete(courseIds,studentIds);
    }
    @GetMapping("/getDeletableStudentsByCourseId/{courseId}")
    public Result getDeletableStudentsByCourseId(@PathVariable("courseId") Long courseId)
    {
        return courseStudentService.getDeletableStudentsByCourseId(courseId);
    }
    @GetMapping("/getDeletableCoursesByStudentId/{studentId}")
    public Result getDeletableCoursesByStudentId(@PathVariable("studentId")Long studentId)
    {
        return getDeletableStudentsByCourseId(studentId);
    }
    @GetMapping("/terms")
    private Result terms()
    {
        return Result.success(Arrays.asList(2021,2022,2023,2024));
    }
    @GetMapping("/teacher")
    public Result getCourseStudentByTeacher(HttpSession session)
    {
        Long teacherId=(Long) session.getAttribute(GlobalConstant.ID);
        if(RoleEnum.TEACHER!=session.getAttribute(GlobalConstant.ROLE))
            throw new RuntimeException("角色异常");
        return courseStudentService.getCourseStudentByTeacherId(teacherId);
    }

}
